package com.seelyn.ddvideo.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/16
 */
@Service
public class MailService {

    private Logger LOG = LoggerFactory.getLogger(MailService.class);
    @Autowired
    JavaMailSender javaMailSender;
    @Autowired
    private Environment env;

    public boolean sendMessageToAdministrator(String content) {
        try {
            final MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
            final MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
            message.setFrom(env.getProperty("spring.mail.username"));
            message.setTo(env.getProperty("spring.mail.toadmin"));
            message.setSubject("凌晨电影网系统消息");
            message.setText(content);
            this.javaMailSender.send(mimeMessage);
            return true;
        } catch (Exception ex) {
            LOG.error(ex.getMessage(), ex);
            return false;
        }
    }

}
